package com.caigou.modules.center.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.bean.Banner
import com.caigou.comm.base.bean.UserInfoBean
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.center.bean.CsQQBean
import com.caigou.modules.center.repository.CenterRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/09/18
 */
@HiltViewModel
class CenterViewModel @Inject constructor(private val repository: CenterRepository) : ViewModel() {

    val bannerLiveData = MutableLiveData<Resource<MutableList<Banner>>>()
    val userInfoLiveData = MutableLiveData<Resource<UserInfoBean>>()
    val buyVipLiveData = MutableLiveData<Resource<String>>()
    val csQQLiveData = MutableLiveData<Resource<CsQQBean>>()

    fun getBanners() {
        viewModelScope.launch(Dispatchers.IO) {
            repository.getBanners()
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MutableList<Banner>>(-1, message)
                    Logger.e(it.message)
                }
                .collectLatest {
                    bannerLiveData.postValue(it)
                }
        }
    }

    fun getCsQQ() {
        csQQLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getCsQQ()
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    csQQLiveData.value = it
                }
        }
    }

    fun getUserInfo(userId: String) {
        userInfoLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getUserInfo(userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<UserInfoBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    userInfoLiveData.value = it
                }
        }
    }

    fun buyVip(amount: Int, userId: String) {
        buyVipLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.bugVip(amount, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    buyVipLiveData.value = it
                }
        }
    }
}